//
//  BK_SP_QR_Cell.m
//  bestkeep
//
//  Created by TOPTEAM on 2017/3/31.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BK_SP_QR_Cell.h"
#import "BK_StorePay_Tool.h"
#import "BK_OD_Color.h"
#import "BKUserStatusInfo.h"
#import <Masonry.h>
#import "Mediator+BKMy.h"
#import "BK_StorePay_VC.h"
#import "BK_Store_BarCode_VC.h"
#import "UIView+ParentController.h"
#import "BK_StorePay_VC.h"
#import "BR_View.h"

@interface BK_SP_QR_Cell ()


@property(nonatomic, strong) UIImageView *ImgV_Member;//会员图标
//@property(nonatomic, strong) UILabel * lab_MemberGrade;//会员等级
@property(nonatomic, strong) UILabel * lab_NoMember;//非会员提示
@property(nonatomic, strong) UILabel * lab_title;//请将二维码出示给营业员

@property(nonatomic, strong) UIImageView *ImgV_qrCode;//二维码图片
@property(nonatomic, strong) UIImageView *ImgV_BKlogo;//BK的logo

@property(nonatomic, strong) UIImageView *ImgV_Update;//刷新图标
@property(nonatomic, strong) UILabel * lab_Update;//刷新


@property (nonatomic,strong) NSString * seed;
@property (nonatomic,strong) NSString * userId;

@property (nonatomic,strong) NSString * userValidTime;



@property (nonatomic,strong) BR_View  *view_BR;


/*
 * 付款码查看
 */
@property(nonatomic, strong) UILabel * lab_BR_Number;
@property(nonatomic, strong) UILabel * lab_BR_Code;

@end


@implementation BK_SP_QR_Cell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self){
        self.backgroundColor = [UIColor whiteColor];
        [self initView];
    }
    return self;
}

- (void)initView{
    

    
    //会员图标
    _ImgV_Member=[[UIImageView alloc]init];
    _ImgV_Member.backgroundColor = [UIColor whiteColor];
//    _ImgV_Member.layer.borderColor = kOrderLineGrey2.CGColor;
//    _ImgV_Member.layer.borderWidth = 0.6;
    _ImgV_Member.contentMode = UIViewContentModeScaleToFill;
//    _ImgV_Member.image=[UIImage imageNamed:@"BK_My_客服头像"];
    [self addSubview:_ImgV_Member];
    [_ImgV_Member mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_top).offset(29/2.0);
        make.left.mas_equalTo(self.mas_left).offset((ScreenWidth-92/2.0)/2.0);
        make.width.mas_equalTo(@(92/2.0));
        make.height.mas_equalTo(@(24/2.0));
    }];
    

    
    //非会员提示
    _lab_NoMember= [UILabel new];
    _lab_NoMember.textColor = kOrderBlack2;
    _lab_NoMember.font = [UIFont boldSystemFontOfSize:14];
    _lab_NoMember.numberOfLines=1;
    _lab_NoMember.text = @"您还不是会员";
    _lab_NoMember.textAlignment=NSTextAlignmentCenter;
    _lab_NoMember.backgroundColor = [UIColor clearColor];
    [self addSubview:_lab_NoMember];
    [_lab_NoMember mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_ImgV_Member.mas_centerY);
        make.left.mas_equalTo(self.mas_left).offset(0);
        make.right.mas_equalTo(self.mas_right).offset(0);
    }];

    
    //请将二维码出示给营业员
    _lab_title= [UILabel new];
    _lab_title.textColor = kOrderBlack2;
    _lab_title.font = [UIFont systemFontOfSize:14];
    _lab_title.numberOfLines=1;
    _lab_title.text = @"请将二维码出示给营业员";
    _lab_title.textAlignment=NSTextAlignmentCenter;
    _lab_title.backgroundColor = [UIColor clearColor];
    [self addSubview:_lab_title];
    [_lab_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_ImgV_Member.mas_bottom).offset(5.0);
        make.left.mas_equalTo(self.mas_left).offset(0);
        make.right.mas_equalTo(self.mas_right).offset(0);
    }];

    

    BKUserStatusInfo * userStatusInfo =[BKUserStatusInfo currentUser];
    if ([userStatusInfo.vipFlag integerValue]==1) {
        /*
         * 如果是VIP
         */
        NSString *Str_ImgName=[NSString stringWithFormat:@"BK_My_Vip%@",userStatusInfo.levelNum];
        _ImgV_Member.image=[UIImage imageNamed:Str_ImgName];
        _lab_NoMember.hidden=YES;
        _ImgV_Member.hidden=NO;
    }else{
        /*
         * 不是VIP
         */
        _ImgV_Member.hidden=YES;
        _lab_NoMember.hidden=NO;
        
    }
    
    /*
     * 二维码
     */
    _ImgV_qrCode = [[UIImageView alloc]init];
    _ImgV_qrCode.backgroundColor = [UIColor clearColor];
    _ImgV_qrCode.contentMode = UIViewContentModeCenter;
    [self addSubview:_ImgV_qrCode];
    [_ImgV_qrCode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_lab_title.mas_bottom).offset(7.0);
        make.left.mas_equalTo(self.mas_left).offset(0);
        make.right.mas_equalTo(self.mas_right).offset(0);
        make.height.mas_equalTo(250);
    }];
    
    /*
     * BK的logo
     */
    _ImgV_BKlogo = [[UIImageView alloc]init];
    _ImgV_BKlogo.backgroundColor = [UIColor clearColor];
    _ImgV_BKlogo.contentMode = UIViewContentModeScaleToFill;
    _ImgV_BKlogo.image=[UIImage imageNamed:@"code_logo"];
    [_ImgV_qrCode addSubview:_ImgV_BKlogo];
    [_ImgV_BKlogo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(_ImgV_qrCode.centerX);
        make.centerY.mas_equalTo(_ImgV_qrCode.centerY);
        make.width.mas_equalTo(93/2.0);
        make.height.mas_equalTo(93/2.0);
    }];
    



    
    
    
    /*
     * 付款码  数字
     */
    _lab_BR_Number= [UILabel new];
    _lab_BR_Number.textColor = kOrderBlack3;
    _lab_BR_Number.font = [UIFont systemFontOfSize:15];
    _lab_BR_Number.numberOfLines=1;
    _lab_BR_Number.text = @"";
    _lab_BR_Number.textAlignment=NSTextAlignmentLeft;
    _lab_BR_Number.backgroundColor = [UIColor clearColor];
    [self addSubview:_lab_BR_Number];
    [_lab_BR_Number mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_ImgV_qrCode.mas_bottom);
        make.left.mas_equalTo(self.mas_left).offset((ScreenWidth-175)/2.0);
    }];
    
    
    
    /*
     * 查看数字
     */
    _lab_BR_Code = [UILabel new];
    _lab_BR_Code.textColor = kOrderBlue1;
    _lab_BR_Code.font = [UIFont systemFontOfSize:14];
    _lab_BR_Code.numberOfLines=1;
    _lab_BR_Code.text = @"查看数字";
    _lab_BR_Code.textAlignment=NSTextAlignmentLeft;
    _lab_BR_Code.backgroundColor = [UIColor clearColor];
    [self addSubview:_lab_BR_Code];
    [_lab_BR_Code mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_ImgV_qrCode.mas_bottom);
        make.left.mas_equalTo(_lab_BR_Number.mas_right).offset(2.0);

    }];
    UITapGestureRecognizer *tap_BR_Code=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(LooK_BR_Code)];
    _lab_BR_Code.userInteractionEnabled=YES;
    [_lab_BR_Code addGestureRecognizer:tap_BR_Code];
    

    
    
    
    
    
    
    
    
    
    //刷新图标
    _ImgV_Update=[[UIImageView alloc]init];
    _ImgV_Update.backgroundColor = [UIColor whiteColor];
    _ImgV_Update.contentMode = UIViewContentModeScaleToFill;
    _ImgV_Update.image=[UIImage imageNamed:@"BK_PayStore_refresh"];
    [self addSubview:_ImgV_Update];
    [_ImgV_Update mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_lab_BR_Number.mas_bottom).offset(17/2.0);
        make.left.mas_equalTo(self.mas_left).offset((ScreenWidth-71.5-14-2)/2.0);
        make.width.mas_equalTo(@(16));
        make.height.mas_equalTo(@(14));
    }];
    _ImgV_Update.userInteractionEnabled = YES;
    UITapGestureRecognizer * Tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(relayOut_QRCode)];
    [_ImgV_Update addGestureRecognizer:Tap];
    
    
    //刷新
    _lab_Update = [UILabel new];
    _lab_Update.textColor = kOrderBKGreen;
    _lab_Update.font = [UIFont systemFontOfSize:14];
    _lab_Update.numberOfLines=1;
    _lab_Update.text = @"付款码刷新";
    _lab_Update.textAlignment=NSTextAlignmentLeft;
    _lab_Update.backgroundColor = [UIColor clearColor];
    [self addSubview:_lab_Update];
    [_lab_Update mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(_ImgV_Update.mas_centerY);
        make.left.mas_equalTo(_ImgV_Update.mas_right).offset(2.0);
        make.right.mas_equalTo(self.mas_right).offset(-ApplicationBackGauge);
    }];
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(relayOut_QRCode)];
    
    _lab_Update.userInteractionEnabled=YES;
    [_lab_Update addGestureRecognizer:tap];
    [self Caculate_time];
    

    
}

-(void)LooK_BR_Code{
    _view_BR=[[BR_View alloc]init];
    [[self lastWindow] addSubview:_view_BR];
    _view_BR.frame = CGRectMake(_lab_BR_Code.frame.origin.x, _lab_BR_Code.frame.origin.y+64, _lab_BR_Code.frame.size.width, _lab_BR_Code.frame.size.height);
    
    UITapGestureRecognizer *tap_BR_Code=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Dismiss_BR_Code)];
    [_view_BR addGestureRecognizer:tap_BR_Code];
    
    
    [UIView animateWithDuration:0.5f animations:^{
        [UIView BKcurrentViewController].navigationController.navigationBarHidden=YES;
        
        
        _view_BR.transform = CGAffineTransformMakeRotation(M_PI_2);

        _view_BR.frame=CGRectMake(0, 0, SCREENWIDTH, ScreenHeight);
        
    } completion:^(BOOL finished) {

        if (_paymentCode) {
            [_view_BR relayOutBRWithNum:_paymentCode];
        }else{
            [_view_BR relayOutBRWithNum:[BK_StorePay_Tool getPayCodeWithSeed:_seed andUserId:_userId andType:_type_pay]];
        }
        
    }];
    

}
-(void)Dismiss_BR_Code{
    [UIView animateWithDuration:0.5f animations:^{
        [UIView BKcurrentViewController].navigationController.navigationBarHidden=NO;
        //        复原
        _view_BR.transform = CGAffineTransformIdentity;
        //        复原第二种方法
        //        self.tranView.transform = CGAffineTransformMakeRotation(2*M_PI);
        
        _view_BR.lab_Tip.hidden=YES;
        _view_BR.lab_Number.hidden=YES;
        _view_BR.ImgV_Tip.hidden=YES;
        [_view_BR setFrame:CGRectMake(_lab_BR_Code.frame.origin.x, _lab_BR_Code.frame.origin.y+64, _lab_BR_Code.frame.size.width, _lab_BR_Code.frame.size.height)];
        
        
        
        [self layoutIfNeeded];

    } completion:^(BOOL finished) {
        [_view_BR removeFromSuperview];
        _view_BR=nil;
    }];
}

- (UIWindow *)lastWindow {
    
    return [UIApplication sharedApplication].keyWindow;
}


-(void)relayOut_QRCode{
    
    BKUserStatusInfo * userStatusInfo =[BKUserStatusInfo currentUser];

    _seed=userStatusInfo.userSecretKey;
    _userId=userStatusInfo.userId;
    
    _paymentCode=[BK_StorePay_Tool getPayCodeWithSeed:_seed andUserId:_userId andType:_type_pay];

    _lab_BR_Number.text = [BR_View GetBR_Code:_paymentCode] ;
    
    
    if ([[UIView BKcurrentViewController] isKindOfClass:[BK_StorePay_VC class]]) {
        BK_StorePay_VC * vc=(BK_StorePay_VC *)[UIView BKcurrentViewController];
        
        vc.paymentCode=_paymentCode;

    }
    /*
     * BR也要同步刷新
     */
    if (_view_BR) {
        [_view_BR relayOutBRWithNum:_paymentCode];
    }

    _ImgV_qrCode.image=[BK_StorePay_Tool Get_QRcodeImG_WithPaymentCode:_paymentCode  andSize:CGSizeMake(250, 250)];

}
/*
 * 切换付款方式
 */
-(void)relayOut_QRType:(NSInteger)type{
    
    BKUserStatusInfo * userStatusInfo =[BKUserStatusInfo currentUser];
    
    _type_pay=type;
    
    if (type==10) {
        for (BKUserPayWay * item in userStatusInfo.userPayWays) {
            if ([item.userPayWayName isEqualToString:@"余额账户"]) {
                _type_pay=[[NSString stringWithFormat:@"%@",item.userPayWayValue] integerValue];
            }
        }
    }else if(type==20){
        for (BKUserPayWay * item in userStatusInfo.userPayWays) {
            if ([item.userPayWayName isEqualToString:@"支付宝账户"]) {
                _type_pay=[[NSString stringWithFormat:@"%@",item.userPayWayValue] integerValue];
            }
        }
    }else if(type==30){
        for (BKUserPayWay * item in userStatusInfo.userPayWays) {
            if ([item.userPayWayName isEqualToString:@"微信账户"]) {
                _type_pay=[[NSString stringWithFormat:@"%@",item.userPayWayValue] integerValue];
            }
        }
    }

    [self relayOut_QRCode];
}


- (void)removeFromSuperview{
    
    [_timer invalidate];
    _timer = nil;
    [super removeFromSuperview];
}


- (void)dealloc{

    [_timer invalidate];
    _timer = nil;
 
}

-(void)Caculate_time{
    NSTimeInterval time = [[NSDate date] timeIntervalSince1970];
    long long int date = (long long int)time;
    NSLog(@"date\n%lld", date); 
    
    BKUserStatusInfo * userStatusInfo =[BKUserStatusInfo currentUser];
    _userValidTime=userStatusInfo.userValidTime;
    
    int t_Width;
    
    
    if (_userValidTime) {
        if ([[NSString stringWithFormat:@"%@",_userValidTime] intValue]>0) {
            t_Width=[[NSString stringWithFormat:@"%@",_userValidTime] intValue];
        }
    }
    if (!t_Width) {
        t_Width=60;
    }
    
    int t=date%t_Width;
    NSLog(@"tttttttttttt->%d",t);
    
//    [self Delay_ActionWithTime:t];
    [self Delay_Action];
    
}

//-(void)Delay_ActionWithTime:(int)time{
//    [self performSelector:@selector(Delay_Action) withObject:nil/*可传任意类型参数*/ afterDelay:time];
//}

-(void)Delay_Action{
//    [self relayOut_QRCode];
    [self Add_Timer];
}


-(void)Add_Timer{
    
    
//    dispatch_async(dispatch_get_global_queue(0, 0), ^{
//        _timer = [NSTimer scheduledTimerWithTimeInterval:6 target:self selector:@selector(relayOut_QRCode) userInfo:@{@"key":@"value"} repeats:true];
//        [[NSRunLoop currentRunLoop] run];
//        
//    });
    
    BKUserStatusInfo * userStatusInfo =[BKUserStatusInfo currentUser];
    _userValidTime=userStatusInfo.userValidTime;
    
    int t_Width;
    
    
    if (_userValidTime) {
        if ([[NSString stringWithFormat:@"%@",_userValidTime] intValue]>0) {
            t_Width=[[NSString stringWithFormat:@"%@",_userValidTime] intValue];
        }
    }
    if (!t_Width) {
        t_Width=60;
    }
    
    if (t_Width>0) {
        
    }else{
        t_Width=60;
    }
    
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:t_Width target:self selector:@selector(relayOut_QRCode) userInfo:nil repeats:YES];
    _timer = timer;
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}


- (void)awakeFromNib {
    [super awakeFromNib];

    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

/**
 *   获取字符宽度
 */
+ (CGFloat)getWidthWithTitle:(NSString *)title font:(CGFloat )font {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1000, 0)];
    label.text = title;
    label.font = [UIFont systemFontOfSize:font];
    [label sizeToFit];
    return label.frame.size.width;
}



+(CGFloat)computeHeight:(id)data{
    return 792/2.0;
}
@end
















































